<?php
/*
 * kernel/object.charset.php
 *
 * This code is distributed and used under the GNU GPL v2 License,
 * please, read file LICENSE, which is situated in root directory,
 * or in "LICENSE" directory with most acceptable translation for you
 *
 * Character sets and internationalization
 *
 * NOTE:
 *  * php mbstring doesn't support KOI8-U charset,
 *    so if you are using it, please create language files
 *    for this encoding
 *
 * Copyright (C) 2010, Taras Kudla
 *
 */

class gCharset extends gKernel {
    /*
     * Get canonical name for charset due to requirement
     */

    /**
     * Gets specified charset name
     * 
     * @param string $cnitem        type of requested object
     * @param string $charsetname   charset name
     * @return string
     */
    public function getcnname($cnitem="browser",$charsetname=null){
        $charset = ($charsetname!=null)?$charsetname:strtolower($this->core->get("charset"));
        $charset =  $this->detectbyname($charset);
        /*
         * Lang files
         */
        if( $cnitem==_CHARSET_CNLANGFILE){
            switch( $charset){
                case _CHARSET_CP1251: return "cp1251";  break;
                case _CHARSET_UTF8:   return "utf8";    break;
                case _CHARSET_KOI8R:  return "koi8r";   break;
                case _CHARSET_KOI8U:  return "koi8u";   break;
            }
        }
        /*
         * Databases
         */
        if( $cnitem==_CHARSET_CNMYSQL){
            switch( $charset){
                case _CHARSET_CP1251: return "cp1251";  break;
                case _CHARSET_UTF8:   return "utf8";    break;
                case _CHARSET_KOI8R:  return "koi8r";   break;
                case _CHARSET_KOI8U:  return "koi8u";   break;
            }
        }
        
        if( $cnitem==_CHARSET_CNPGSQL){
            switch( $charset){
                case _CHARSET_CP1251: return "WIN1251"; break;
                case _CHARSET_UTF8:   return "UTF8";    break;
                case _CHARSET_KOI8R:  return "KOI8";    break;
                case _CHARSET_KOI8U:  return "KOI8";    break;
            }
        }
        if( $cnitem==_CHARSET_CNFIREBIRD){
            switch( $charset){
                case _CHARSET_CP1251: return "WIN1251"; break;
                case _CHARSET_UTF8:   return "UTF8";    break;
                case _CHARSET_KOI8R:  return "KOI8";    break;
                case _CHARSET_KOI8U:  return "KOI8";    break;
            }
        }
    }

    /**
     * Detects charset name by specified
     * 
     * @param string $charset
     * @return string
     */
    public function detectbyname($charset){
        if( $charset=="windows-1251" || $charset=="cp1251" || $charset=="win1251"){
                return _CHARSET_CP1251;
        }
        if( $charset=="utf8" || $charset=="utf-8"){
            return _CHARSET_UTF8;
        }
        if( $charset=="koi8r" || $charset=="koi8-r" || $charset==_CHARSET_KOI8R){
            return _CHARSET_KOI8R;
        }
        if( $charset=="koi8u" || $charset=="koi8-u" || $charset==_CHARSET_KOI8U){
            return _CHARSET_KOI8U;
        }
        /*
         * By default
         */
        return _CHARSET_UTF8;
    }

    /**
     * Converts text from charset to another
     * 
     * @param string $text  Specified text
     * @param string $to    Destination charset
     * @param string $from  Source charset
     * @return string
     */
    public function converttext($text,$to=null,$from = null){

        $usembstring = false;//extension_loaded("mbstring");
        if($from == null){
            $from = _CHARSET_UTF8;
        }
        if( $to==null){
            $to = $this->core->get("charset");
        }
        return mb_convert_encoding($text,$to, $from);
    }

    /**
     * Converts UTF8 text to KOI8u
     * 
     * @param string $text
     * @return string
     */
    public function utf8tokoiu(&$text){
        $text = str_replace( "?", "[question_mark]", $text );
        $text = mb_convert_encoding($text, _CHARSET_KOI8R, _CHARSET_UTF8);
        //$text = str_replace("?",ord(0x186),$text);
        $text = str_replace( "[question_mark]", "?", $text );
        return $text;
    }

    /**
     * Gets converted text from specified file
     *
     * @param string $filename  Filename
     * @param string $to        Destination charset
     * @param string $from      Source charset
     * @return string>
     */
    public function gettextfile($filename,$to=null,$from=null){
        # getting file data
        if( !file_exists($filename)){
            $this->core->error("charset","File for charset converting not found ($filename)",_ERR_WARNING,__FILE__,__LINE__);
            return false;
        }
        $text = file_get_contents($filename);
        return $this->converttext($text,$to,$from);
    }
}